package org.whh.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whh.auth.WhhPermission;
import org.whh.base.util.AjaxResult;
import org.whh.base.util.ResultDTO;
import org.whh.dto.result.RoleDTO;
import org.whh.org.domain.Employee;
import org.whh.org.service.IEmployeeService;
import org.whh.base.util.QueryVo;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理",tags = "员工管理")
@WhhPermission(name = "employy",desc = "wosi emp")
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;
    @PutMapping
    @ApiOperation(value = "添加或者更新方法")
    @WhhPermission(name = "employy1",desc = "wosi em1")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()!=null){
                employeeService.update(employee);
            }else {
                employeeService.insert(employee);
            }
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作出错"+e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除方法")
    public AjaxResult delete(@PathVariable("id") @ApiParam(value = "主键",required = true) Long id){
        try {
            employeeService.deleteById(id);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工方法")
    public AjaxResult getById(@ApiParam(value = "主键",required = true) @PathVariable("id") Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return new AjaxResult().getself().setObj(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有员工方法")
    public AjaxResult getAll(){
        try {
            List<Employee> employees = employeeService.getAll();
            return new AjaxResult().getself().setObj(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询用户方法")
    public AjaxResult getPage(@RequestBody QueryVo queryVo){
        try {
            System.out.println(queryVo);
            ResultDTO<Employee> result = employeeService.getAllPage(queryVo);
            return new AjaxResult().getself().setObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("分页查询出错"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除方法")
    public AjaxResult batchDelete(@ApiParam(value = "主键",required = true)@RequestBody List<Long> ids){
        try {
            System.out.println("批量删除方法");
            employeeService.batchDelete(ids);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    @PostMapping("/role")
    public AjaxResult setEmployeeRole(@RequestBody RoleDTO dto){
        try {
            System.out.println("dto");
            System.out.println(dto);
            employeeService.setEmployeeRole(dto);
            return AjaxResult.getself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("设置角色失败!"+e.getMessage());
        }
    }

    @GetMapping("/id/{employeeId}")
    @ApiOperation(value = "通过employeeId查询roleId")
    @WhhPermission(name="通过employeeId查询roleId",desc = "我是通过employeeId查询roleId")
    public AjaxResult getRoleIdByEmployeeId(@ApiParam(value = "主键",required = true) @PathVariable("employeeId") Long employeeId){
        try {
            List<Long> ids = employeeService.getRoleIdByEmployeeId(employeeId);
            return new AjaxResult().getself().setObj(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }
}
